package com.rongshu.view.userInfo.activity;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.rongshu.R;
import com.rongshu.utils.Common;
import com.rongshu.utils.Constant;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.userInfo.entity.AboutUs;
import com.rongshu.view.userInfo.iview.AboutUsView;
import com.rongshu.view.userInfo.presenter.AboutUsPresenter;
import com.rongshu.view.web.LocalWebActivity;
import com.utils.ApkUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class AboutUsAcitivity extends BaseActivity<AboutUsPresenter> implements AboutUsView {
    @BindView(R.id.iv_icon)
    ImageView iv_icon;
    @BindView(R.id.txt_version)
    TextView txt_version;
    @BindView(R.id.txt_wechat_official_account)
    TextView txt_wechat_official_account;
    @BindView(R.id.txt_xinlangweibo_xyrs)
    TextView txt_xinlangweibo_xyrs;
    @BindView(R.id.txt_company_web)
    TextView txt_company_web;

    //intent
    private Intent intent = null;
    //map
    Map<String, Object> map = null;

    @Override
    protected int initLayout() {
        return R.layout.activity_about_us_view;
    }

    @Override
    protected void initView() {
        //set present
        setPresenter(new AboutUsPresenter(this), this);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void updateViews(boolean isRefresh) {
        getAboutUs();
    }

    /**
     * 刷新view
     */
    private void refreshView(AboutUs.ResponseBean responseBean) {
        //头像
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .placeholder(R.drawable.icon);
        Glide.with(this)
                .load(responseBean.getImage())
                .apply(options)
                .into(iv_icon);
        //version
        txt_version.setText("V" + ApkUtils.versionName(this));
        //微信公众号
        txt_wechat_official_account.setText("微信公众号: " + responseBean.getWechat_official_account());
        //新浪微博
        txt_xinlangweibo_xyrs.setText("新浪微博: " + responseBean.getSina_microblog());
        //网站
        txt_company_web.setText("网站: " + responseBean.getWeburl());
    }

    /**
     * 关于我们
     */
    private void getAboutUs() {
        map = new HashMap<>();
        map.put("rnd", 0);
        presenter.getAbout(
                0, Common.encryptionSign(map));
        map.clear();
    }


    @OnClick({R.id.layout_back, R.id.layout_about_us, R.id.layout_user_convention, R.id.layout_privacy})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_back:
                finish();
                break;
            case R.id.layout_about_us:
                intent = new Intent(AboutUsAcitivity.this, AboutUsInformationActivity.class);
                startActivity(intent);
                break;
            case R.id.layout_user_convention:  //用户协议
                intent = new Intent(AboutUsAcitivity.this, LocalWebActivity.class);
                intent.putExtra(Constant.INTENT_TITLE, "服务协议");
                intent.putExtra(Constant.INTENT_H5, Common.USER_AGREEMETN);
                startActivity(intent);
                break;
            case R.id.layout_privacy:
                intent = new Intent(AboutUsAcitivity.this, LocalWebActivity.class);
                intent.putExtra(Constant.INTENT_TITLE, "隐私政策");
                intent.putExtra(Constant.INTENT_H5, Common.PRIVACY_POLICY);
                startActivity(intent);
                break;
        }
    }

    /**
     * 关于我们回调
     */
    @Override
    public void onAboutUs(AboutUs aboutUs) {
        if (aboutUs.getErrCode() == 0) {
            refreshView(aboutUs.getResponse());
        } else {
            toast(aboutUs.getErrMsg());
        }
    }
}
